package com.wechat.order.controller.SellerController;

import com.google.common.collect.Maps;
import com.wechat.order.constant.CookieConstant;
import com.wechat.order.constant.RedisConstant;
import com.wechat.order.dataobject.SellerInfo;
import com.wechat.order.service.SellerService.SellerInfoService;
import com.wechat.order.util.CookieUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 买家登陆登陆
 * Created by zzy on 2017/12/20.
 */
@Controller
@RequestMapping("/seller")
public class SellerUserController {

    @Autowired
    private SellerInfoService sellerInfoService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 扫码登陆目标控制
     * @param openid
     * @param response
     * @return
     */
    @GetMapping("/login")
    public ModelAndView login(@RequestParam("openid") String openid,
                              HttpServletResponse response){
        //1、去和数据库匹配
        SellerInfo sellerInfo=sellerInfoService.findSellerInfoByOpenid(openid);
        Map model= Maps.newHashMap();
     if(sellerInfo==null){
         model.put("msg","登陆失败");
         model.put("url","/seller/order/list");
         return new ModelAndView("common/error",model);
     }
        String token = UUID.randomUUID().toString();
        Integer expire= RedisConstant.LOGIN_EXPIRE;
        //2、设置token至redis
        stringRedisTemplate.opsForValue().set(String.format(RedisConstant.TOKEN_PREFIX,token),openid,expire, TimeUnit.SECONDS);
        //3、设置token至cookie
        CookieUtil.set(response,"token",token,expire);
        return new ModelAndView("redirect:/seller/order/list");

    }

    /**
     * 同一登出控制
     * @param request
     * @param response
     * @param model
     * @return
     */
    @GetMapping("/logout")
    public ModelAndView logout(HttpServletRequest request,HttpServletResponse response,
                       Map model){
        //从cookie查询

       Cookie cookie= CookieUtil.get(request, CookieConstant.TOKEN);
       if(cookie!=null){
           String token=cookie.getValue();
           stringRedisTemplate.opsForValue().getOperations().delete(String.format(RedisConstant.TOKEN_PREFIX,token));
           //清除cookie
           CookieUtil.delCookie(response,cookie.getName());
       }
        model.put("msg","登出成功");
        model.put("url","/sell/seller/order/list");
        return  new ModelAndView("/common/success",model);
    }
}
